package com.chad.library.adapter.base.viewholder;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Checkable;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import me.zhouzhuo810.magpiex.utils.ColorUtil;

/**
 * ViewHolder 基类<br>
 *
 * 改用java实现，用kotlin实现，在
 * Android Studio Chipmunk | 2021.2.1<br>
 * Build #AI-212.5712.43.2112.8512546, built on April 29, 2022<br>
 * 版本无法调用继承的{@link RecyclerView.ViewHolder}方法
 *
 * @author Created by wanggaowan on 2022/5/16 10:02
 */
public class BaseViewHolder extends RecyclerView.ViewHolder {
    
    /**
     * Views indexed with their IDs
     */
    private final SparseArray<View> views = new SparseArray<>();
    
    public BaseViewHolder(@NonNull View itemView) {
        super(itemView);
    }
    
    @SuppressWarnings("unchecked")
    @Nullable
    public <T extends View> T getView(@IdRes int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            T t = itemView.findViewById(viewId);
            if (t != null) {
                views.put(viewId, t);
                return t;
            }
        }
        return (T) view;
    }
    
    @NonNull
    public <T extends View> T getViewNonNull(@IdRes int viewId) {
        T t = getView(viewId);
        if (t == null) {
            throw new IllegalStateException("No view found with id " + viewId);
        }
        return t;
    }
    
    @Nullable
    public <T extends View> T findView(@IdRes int viewId) {
        return itemView.findViewById(viewId);
    }
    
    public BaseViewHolder setText(@IdRes int viewId, CharSequence value) {
        TextView view = getView(viewId);
        if (view != null) {
            view.setText(value);
        }
        return this;
    }
    
    public BaseViewHolder setText(@IdRes int viewId, @StringRes int strId) {
        TextView view = getView(viewId);
        if (view != null) {
            view.setText(strId);
        }
        return this;
    }
    
    public BaseViewHolder setTextColor(@IdRes int viewId, @ColorInt int color) {
        TextView view = getView(viewId);
        if (view != null) {
            view.setTextColor(color);
        }
        return this;
    }
    
    public BaseViewHolder setTextColorRes(@IdRes int viewId, @ColorRes int colorRes) {
        TextView view = getView(viewId);
        if (view != null) {
            view.setTextColor(ContextCompat.getColor(itemView.getContext(), colorRes));
        }
        return this;
    }
    
    public BaseViewHolder setImageResource(@IdRes int viewId, @DrawableRes int imageResId) {
        ImageView view = getView(viewId);
        if (view != null) {
            view.setImageResource(imageResId);
        }
        return this;
    }
    
    public BaseViewHolder setImageDrawable(@IdRes int viewId, Drawable drawable) {
        ImageView view = getView(viewId);
        if (view != null) {
            view.setImageDrawable(drawable);
        }
        return this;
    }
    
    public BaseViewHolder setImageBitmap(@IdRes int viewId, Bitmap bitmap) {
        ImageView view = getView(viewId);
        if (view != null) {
            view.setImageBitmap(bitmap);
        }
        
        return this;
    }
    
    public BaseViewHolder setIconColor(@IdRes int viewId, int color) {
        ImageView view = getView(viewId);
        if (view != null) {
            ColorUtil.setIconColor(view, color);
        }
        
        return this;
    }
    
    public BaseViewHolder setBackgroundColor(@IdRes int viewId, @ColorInt int color) {
        View view = getView(viewId);
        if (view != null) {
            view.setBackgroundColor(color);
        }
        
        return this;
    }
    
    public BaseViewHolder setBackgroundResource(@IdRes int viewId, @DrawableRes int backgroundRes) {
        View view = getView(viewId);
        if (view != null) {
            view.setBackgroundResource(backgroundRes);
        }
        
        return this;
    }
    
    public BaseViewHolder setAlpha(@IdRes int viewId, float alpha) {
        View view = getView(viewId);
        if (view != null) {
            view.setAlpha(alpha);
        }
        
        return this;
    }
    
    public BaseViewHolder setVisible(@IdRes int viewId, boolean isVisible) {
        View view = getView(viewId);
        if (view != null) {
            view.setVisibility(isVisible ? View.VISIBLE : View.INVISIBLE);
        }
        
        return this;
    }
    
    public BaseViewHolder setGone(@IdRes int viewId, boolean isVisible) {
        View view = getView(viewId);
        if (view != null) {
            view.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        }
        
        return this;
    }
    
    public BaseViewHolder setEnabled(@IdRes int viewId, boolean isEnabled) {
        View view = getView(viewId);
        if (view != null) {
            view.setEnabled(isEnabled);
        }
        
        return this;
    }
    
    public BaseViewHolder setChecked(@IdRes int viewId, boolean isChecked) {
        View view = getView(viewId);
        if (view instanceof Checkable) {
            ((Checkable) view).setChecked(isChecked);
        }
        
        return this;
    }
    
    public BaseViewHolder setOnTouchListener(@IdRes int viewId, @NonNull View.OnTouchListener onTouchListener) {
        View view = getView(viewId);
        if (view != null) {
            view.setOnTouchListener(onTouchListener);
        }
        
        return this;
    }
    
    public BaseViewHolder setOnCheckedChangeListener(@IdRes int viewId,
                                                     @NonNull CompoundButton.OnCheckedChangeListener onCheckedChangeListener) {
        CompoundButton view = getView(viewId);
        if (view != null) {
            view.setOnCheckedChangeListener(onCheckedChangeListener);
        }
        
        return this;
    }
    
    public BaseViewHolder setTextViewMinWidth(@IdRes int viewId, int sizePx) {
        TextView view = getView(viewId);
        if (view != null) {
            view.setMinWidth(sizePx);
        }
        
        return this;
    }
    
    public BaseViewHolder setHintText(@IdRes int viewId, @NonNull CharSequence hint) {
        TextView view = getView(viewId);
        if (view != null) {
            view.setHint(hint);
        }
        
        return this;
    }
    
    public BaseViewHolder setSelected(@IdRes int viewId, boolean selected) {
        View view = getView(viewId);
        if (view != null) {
            view.setSelected(selected);
        }
        
        return this;
    }
    
    public BaseViewHolder setTextSizePx(@IdRes int viewId, float sizePx) {
        TextView view = getView(viewId);
        if (view != null) {
            view.setTextSize(TypedValue.COMPLEX_UNIT_PX, sizePx);
        }
        
        return this;
    }
    
    public BaseViewHolder setTextSizeDp(@IdRes int viewId, float sizeDp) {
        TextView view = getView(viewId);
        if (view != null) {
            view.setTextSize(TypedValue.COMPLEX_UNIT_DIP, sizeDp);
        }
        
        return this;
    }
    
    public BaseViewHolder setTextViewMinHeight(@IdRes int viewId, int sizePx) {
        TextView view = getView(viewId);
        if (view != null) {
            view.setMinHeight(sizePx);
        }
        
        return this;
    }
    
    public BaseViewHolder setBackgroundDrawableColor(@IdRes int viewId, @ColorInt int color) {
        View view = getView(viewId);
        if (view != null) {
            Drawable background = view.getBackground();
            if (background instanceof GradientDrawable) {
                GradientDrawable mutate = (GradientDrawable) background.mutate();
                mutate.setColor(color);
            }
        }
        
        return this;
    }
    
    public BaseViewHolder setWidthAndHeight(@IdRes int viewId, int widthPx, int heightPx) {
        View view = getView(viewId);
        if (view != null) {
            LayoutParams layoutParams = view.getLayoutParams();
            if (layoutParams != null) {
                boolean hasChange = false;
                if (layoutParams.width != widthPx) {
                    layoutParams.width = widthPx;
                    hasChange = true;
                }
                
                if (layoutParams.height != heightPx) {
                    layoutParams.height = heightPx;
                    hasChange = true;
                }
                
                if (hasChange) {
                    view.setLayoutParams(layoutParams);
                }
            }
        }
        
        return this;
    }
}
